// pages/MHome/col/collist.js
const db = wx.cloud.database();
var colmap = new Map();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collist:[],
    randstr:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options.randstr)
  var randstr = options.randstr
    //var randstr = '5251';
    db.collection('colduty').where({
      randstr:parseInt(randstr)
    }).get().then(res=>{
      console.log(res)
      var collist = res.data[0].collist;
      _.setData({
        collist:collist,
        randstr:randstr
      })
    })
  },
  getinput(e){
    console.log(e)
    var key = e.currentTarget.dataset.name;
    var value = e.detail.value;
    colmap.set(key,value);
  },
  submit(){
    var _ = this;
    var tcol = _.data.collist;
    var len = tcol.length;
    var coledlen = colmap.size;
    console.log(coledlen)
    if(len!=coledlen){
      wx.showToast({
        title: '请填写完全',
      })
      return
    }
    var coledlist = [];
    for(var i = 0; i < len; i++){
      coledlist.push(colmap.get(tcol[i]))
    }
    db.collection('coltmp').add({
      data:{
        ranstr:_.data.randstr,
        collist:tcol,
        coledlist:coledlist
      }
    }).then(res=>{
      console.log(res)
      wx.navigateBack({
        complete: (res) => {},
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})